package io.bluefw.microapp.auth.resource;

import java.net.URISyntaxException;

import javax.ws.rs.Consumes;
import javax.ws.rs.FormParam;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.core.Response;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.UsernamePasswordToken;

import io.dropwizard.hibernate.UnitOfWork;

@Path("/auth/login")
public class LoginResource {
	@POST
    @Consumes("application/x-www-form-urlencoded")
	@UnitOfWork
	public Response login(
			@FormParam("username") String username, 
			@FormParam("password") String password, 
			@FormParam("rememberMe") Boolean rememberMe) throws URISyntaxException {
	    if (rememberMe == null) {
	    	rememberMe = false;
	    } 
	    if (username == null || password == null) {
	      return Response.serverError().entity("username and password required").build();
	    }

	    SecurityUtils.getSubject().login(new UsernamePasswordToken(username, password, rememberMe));
	    
	    //URI uri = UriBuilder.fromUri("/login.html").build();
	    //return Response.seeOther(uri).build();
	    //return Response.temporaryRedirect(uri).build();
	    return Response.ok(username).build();
	}

}
